%% This document created by Scientific Word (R) Version 3.0

\documentclass[a4paper,twoside]{book}
\usepackage[latin1]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{latexsym}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{amssymb}
%TCIDATA{OutputFilter=latex2.dll}
%TCIDATA{CSTFile=book.cst}
%TCIDATA{LastRevised=Thu Jan 20 13:27:05 2000}
%TCIDATA{<META NAME="GraphicsSave" CONTENT="32">}
\setlength{\oddsidemargin}{0.cm}
\setlength{\evensidemargin}{0.cm}
\setlength{\textwidth}{13.cm}
\setlength{\textheight}{16.cm}
\setlength{\parskip}{0.15cm}
\setlength{\parindent}{0.pt}
\newenvironment{linea}{
\begin{picture}(300,10)
\line(300,0){300}
\end{picture}}{ }
\newenvironment{Ej E/S}
               {\setlength{\parskip}{0.0cm}}
               {\endlist}
\newenvironment{Lista letras}
               {\list{\alph{letra}.}{
                        \usecounter{letra}
                        \itemsep     0.0cm
                        \itemindent   0.0cm
                        \listparindent 0.0cm
                        \itemindent 0.0cm
                        \rightmargin  0.5cm
                        \leftmargin 1.0cm
                        \labelwidth 0.5cm
                        \parsep 0.0cm
                        \topsep 0.0cm
                        \parskip 0.0cm}}
               {\endlist}
\newenvironment{Programa}
               {\ttfamily\small\list{\arabic{conta}.}{\usecounter{conta}
                        \itemsep     0.0cm
                        \itemindent   0.0cm
                        \listparindent 0.0cm
                        \itemindent 0.0cm
                        \rightmargin  0.7cm
                        \leftmargin 1.0cm
                        \labelwidth 0.5cm
                        \parsep 0.0cm
                        \topsep 0.0cm
                        \parskip 0.0cm}}
               {\endlist}
\newenvironment{Programa-cuadro}
               {\begin{lrbox}{\savepar}
                \begin{minipage}[b]{13.cm}
                \ttfamily\list{\arabic{conta}.}{\usecounter{conta}
                        \itemsep     0.0cm
                        \itemindent   0.0cm
                        \listparindent 0.0cm
                        \itemindent 0.0cm
                        \rightmargin  0.7cm
                        \leftmargin 1.0cm
                        \labelwidth 0.5cm
                        \parsep 0.0cm
                        \topsep 0.0cm
                        \parskip 0.0cm}}
               {\endlist\end{minipage}\end{lrbox}\fbox{\usebox{\savepar}}}
\newenvironment{Lista guiones}{
     \begin{list}{-}{
     \setlength{\leftmargin}{0.5cm}
     \setlength{\labelwidth}{0.5cm}
     \setlength{\parsep}{0.pt}
     \setlength{\topsep}{0.0cm}
     \setlength{\parsep}{0.pt}
     \setlength{\itemsep}{0.pt}
     \setlength{\parskip}{0.pt}}
   }
   {\end{list}}
\newsavebox{\savepar}
\newenvironment{recuadro}{
\begin{lrbox}{\savepar}
\begin{minipage}[b]{13.cm}
}
{\end{minipage}\end{lrbox}\fbox{\usebox{\savepar}}}
\newenvironment{Solucion}{\newline \Large {\textbf {SOLUCI\'ON}}}{ }
\newcommand{\enunciado}{\Large{\textbf{ENUNCIADO}}}

\begin{document}
\newcounter{conta} \newcounter{letra} \setcounter{letra}{1}

{\Large {\textbf{ENUNCIADO}} }

Escribir un programa, utilizando LEX y YACC, que tenga como entrada un fichero
con expresiones de asignaci\'{o}n y expresiones que utilizan operadores
relacionales. S\'{o}lo se puede utilizar, como nombre de variables o
identificadores una letra min\'{u}scula del alfabeto ingl\'{e}s (en total, 26
variables distintas). Los valores que se les pueden asignar son n\'{u}meros
reales sin parte exponencial. Los operadores relacionales que se pueden
utilizar son: $<,>,<=,>=,==,!=$. El operador de asignaci\'{o}n es $=$. Cada
expresi\'{o}n est\'{a} en una l\'{i}nea diferente. El siguiente es un ejemplo
de fichero de entrada:

\begin{Ej E/S}
\qquad\qquad\qquad\qquad x=4.3

\qquad\qquad\qquad\qquad x%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
5.0

\qquad\qquad\qquad\qquad y=9.6

\qquad\qquad\qquad\qquad x!=y

\qquad\qquad\qquad\qquad2.0==5.0

\qquad\qquad\qquad\qquad z%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
=7.2
\end{Ej E/S}

La salida que se quiere obtener es, en funci\'{o}n del tipo de expresi\'{o}n
que haya en la l\'{i}nea, la siguiente:

\begin{Lista guiones}
\item  para expresiones de asignaci\'{o}n, indicar el valor que se ha asignado
a la variable

\item  para expresiones con operadores relacionales, indicar si es verdadera o falsa

\item  en caso de que una expresi\'{o}n no pueda evaluarse, la causa de este error

\item  cuando se utilice una variable en una expresi\'{o}n con operadores
relacionales deber\'{a} comprobarse que se le ha asignado previamente un valor.
\end{Lista guiones}

Por ejemplo, para el fichero de entrada anterior, la salida deber\'{i}a ser:

\begin{Ej E/S}
\qquad\qquad La variable x toma el valor 4.3

\qquad\qquad Esta expresi\'{o}n es Verdadera

\qquad\qquad La variable y toma el valor 9.6

\qquad\qquad Esta expresi\'{o}n es Verdadera

\qquad\qquad Esta expresi\'{o}n es Falsa

\qquad\qquad La variable z no est\'{a} definida
\end{Ej E/S}

Se pide:

\begin{Lista letras}
\item  Escribir el analizador lexicogr\'{a}fico utilizando LEX

\item  Escribir el analizador sint\'{a}ctico que produzca la salida
especificada, utilizando YACC.
\end{Lista letras}

{\Large {\textbf{SOLUCI\'{O}N}} }

\begin{Lista letras}
\item  Analizador lexicogr\'{a}fico
\end{Lista letras}

\begin{Programa}
\item \%\{

\item \#define\ MENI\ 1

\item \#define\ MEN\ 2

\item \#define\ MAY\ 3

\item \#define\ MAYI\ 4

\item \#define\ DIF\ 5

\item \#define\ IGUAL\ 6

\item \%\}

\item \%\%

\item \texttt{[a-z]}\ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.pos =
yytext\texttt{[0]}-'a';\ return\ ID;\ \}

\item \texttt{[0-9]}+"."\texttt{[0-9]}*\ \ \ \{yylval.valor = atof(yytext);\ return\ NUM;\}

\item =\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{return\ ASIG;\}

\item ==\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.op = IGUAL;\ return\ OP\_REL;\}

\item \texttt{"}%
%TCIMACRO{\TEXTsymbol{>}}%
%BeginExpansion
$>$%
%EndExpansion
="\ \ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.op = MAYI;\ return\ OP\_REL;\}

\item \texttt{"}%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
="\ \ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.op = MENI;\ return\ OP\_REL;\}

\item \texttt{"}%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
"\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.op = MEN;return\ OP\_REL;\}

\item \texttt{"}%
%TCIMACRO{\TEXTsymbol{>}}%
%BeginExpansion
$>$%
%EndExpansion
"\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.op = MAY;\ return\ OP\_REL;\}

\item \texttt{"}!="\ \ \ \ \ \ \ \ \ \ \ \ \ \ \{yylval.op = DIF;return\ OP\_REL;\}

\item \texttt{[\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
t%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n]}\ \ \ \ \ \ \ \ \ \ \ \{;\}

\item
.\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{printf("Error\ en\ la\ entrada\ \%c",yytext\texttt{[0]}%
);\}

\item \%\%
\end{Programa}

\begin{Lista letras}
\item [b.]Analizador sint\'{a}ctico
\end{Lista letras}

\begin{Programa}

\item \%union\ \{

\item \ \ \ \ int\ pos;

\item \ \ \ \ int\ op;

\item \ \ \ \ float\ valor;

\item \ \ \ \ struct\ Variab\ \{

\item \ \ \ \ \ \ \ \ int\ pos;

\item \ \ \ \ \ \ \ \ float\ val;

\item \ \ \ \ \ \ \ \ int\ tipo;

\item \ \ \ \ \}\ Variab;

\item \}

\item \%token\ ASIG

\item \%token\ <pos>\ ID

\item \%token\ <valor>\ NUM

\item \%token\ <op>\ OP\_REL

\item \%type\ <Variab>\ var

\item \%\%

\item  S\ \ \ :\ exp\ \ \{\ /*ignorar*/\ \}

\item \ \ \ \ |\ S\ exp\ \ \{\ /*ignorar*/\ \}

\item \ \ \ \ ;

\item  exp\ :\ ID\ ASIG\ var\ \{

\item \ \ \ \ \ \ \ \ \ \ if\ (\$3.tipo==2\ \&\&\ tab\texttt{[\$3.pos]}.def==0)\{

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf("La\ variable\ \%c\ no\ est\'{a}
definida%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n",\$3.pos+'a');

\item \ \ \ \ \ \ \ \ \ \ \}\ else\ \{

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ (\$3.pos==2)

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tab\texttt{[\$1]}%
.valor=tab\texttt{[\$3.pos]}.valor;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ else

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tab\texttt{[\$1]}.valor\ =\ \$3.val;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ tab\texttt{[\$1]}.def\ =\ 1;

\item
\ \ \ \ \ \ \ \ \ \ \ \ \ \ printf\ ("La\ variable\ \%c\ toma\ el\ valor\ \%3.1f\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n",

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$1+'a',tab\texttt{[\$1]}.valor);

\item \ \ \ \ \ \ \ \ \ \ \}

\item \ \ \ \ \ \ \}

\item \ \ \ \
%TCIMACRO{\TEXTsymbol{\vert}}%
%BeginExpansion
$\vert$%
%EndExpansion
\ var\ OP\_REL\ var\ \{

\item \ \ \ \ \ \ \ \ \ \ float\ val1,val2;

\item \ \ \ \ \ \ \ \ \ \ int\ primera,segunda;

\item \ \ \ \ \ \ \ \ \ \ if\ (\ (\$1.tipo==2)\&\&(tab\texttt{[\$1.pos]}.def==0)\ )\{

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf\ ("La\ variable\ \%c\ no\ est\'{a}%
\ definida%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n",\$1.pos+'a');

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ primera\ =\ 0;

\item \ \ \ \ \ \ \ \ \ \ \}\ else\ \{

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ (\$1.tipo==2)

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val1=tab\texttt{[\$1.pos]}.valor;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ else

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val1\ =\ \$1.val;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ primera\ =\ 1;

\item \ \ \ \ \ \ \ \ \ \}

\item \ \ \ \ \ \ \ \ \ if\ (\ (\$3.tipo==2)\&\&(tab\texttt{[\$3.pos]}.def==0)\ )\{

\item \ \ \ \ \ \ \ \ \ \ \ \ \ printf\ ("La\ variable\ \%c\ no\ est\'{a}%
\ definida%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n",\$3.pos+'a');

\item \ \ \ \ \ \ \ \ \ \ \ \ \ segunda\ =\ 0;

\item \ \ \ \ \ \ \ \ \ \}

\item \ \ \ \ \ \ \ \ \ else\ \{

\item
\ \ \ \ \ \ \ \ \ \ \ \ \ if\ (\$3.tipo==2)\ val2\ =\ tab\texttt{[\$3.pos]}.valor;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ else

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val2\ =\ \$3.val;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ segunda\ =\ 1;

\item \ \ \ \ \ \ \ \ \ \}

\item \ \ \ \ \ \ \ \ \ if\ (primera\ \&\&\ segunda)\ escribe\_comparacion\ (\$2,val1,val2);

\item \ \ \ \ \ \ \}

\item \ \ \ \ ;

\item  var\ :\ NUM\ \ \{\ \$\$.tipo=1;\ \$\$.val=\$1;\}

\item \ \ \ \
%TCIMACRO{\TEXTsymbol{\vert}}%
%BeginExpansion
$\vert$%
%EndExpansion
\ ID\ \ \ \ \{\ \$\$.tipo=2;\ \$\$.pos=\$1;\}

\item \ \ \ \ ;

\item \%\%

\item \#include\ "l1lex.h"

\item  typedef\ struct\ Tabla\ \{

\item \ \ \ float\ valor;

\item \ \ \ int\ def;

\item \}\ Tabla;

\item  Tabla\ tab\texttt{[26]};

\item  extern\ FILE\ *\ yyin;

\item  main()

\item \{

\item \ \ \ \ int\ i;

\item \ \ \ \ for\ (i=0;i%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
26;++i)\ tab\texttt{[i]}.def\ =\ 0;

\item \ \ \ \ yyin\ =\ fopen("L1.IN","r");

\item \ \ \ \ yyparse();

\item \}

\item  void\ escribe\_comparacion\ (int\ oper,float\ x,\ float\ y)\ \{

\item \ \ \ \ switch(oper)\{

\item \ \ \ \ \ \ \ case\ 1:\ printf\ ("Esta\ expresi\'{o}n\ es\ ");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (x%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
=y)?\ printf("VER\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n"):printf("FAL%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;

\item \ \ \ \ \ \ \ case\ 2:\ printf\ ("Esta\ expresi\'{o}n\ es\ ");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (x%
%TCIMACRO{\TEXTsymbol{<}}%
%BeginExpansion
$<$%
%EndExpansion
y)?\ printf("VER\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n"):printf("FAL%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;

\item \ \ \ \ \ \ \ case\ 3:\ printf\ ("Esta\ expresi\'{o}n\ es\ ");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (x%
%TCIMACRO{\TEXTsymbol{>}}%
%BeginExpansion
$>$%
%EndExpansion
y)?\ printf("VER\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n"):printf("FAL%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;

\item \ \ \ \ \ \ \ case\ 4:\ printf\ ("Esta\ expresi\'{o}n\ es\ ");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (x%
%TCIMACRO{\TEXTsymbol{>}}%
%BeginExpansion
$>$%
%EndExpansion
=y)?\ printf("VER\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n"):printf("FAL%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;

\item \ \ \ \ \ \ \ case\ 5:\ printf\ ("Esta\ expresi\'{o}n\ es\ ");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (x!=y)?\ printf("VER\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n"):printf("FAL%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;

\item \ \ \ \ \ \ \ case\ 6:\ printf\ ("Esta\ expresi\'{o}n\ es\ ");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (x==y)?\ printf("VER\
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n"):printf("FAL%
%TCIMACRO{\TEXTsymbol{\backslash}}%
%BeginExpansion
$\backslash$%
%EndExpansion
n");

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;

\item \ \ \ \ \ \ \ \}

\item \}
\end{Programa}
\end{document}